package com.cskaoyan.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.order.biz.Bean.CommonBO;
import com.cskaoyan.order.biz.Bean.DataDTO;
import com.cskaoyan.order.biz.Bean.ShoppingOrderVO;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderItemDto;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.dto.OrderShippingDto;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxgl
 * @date 2023-01-10 00:51
 * @describe
 */
@Service
@Slf4j
public class OrderQueryServiceImpl1 implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;


    @Autowired
    OrderConverter orderConverter;


    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        return null;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId,Long uid) {

        Order order = orderMapper.selectByPrimaryKey(orderId);
//        Member member = orderMapper.selectAddressByUid(uid);
        OrderShipping orderShipping = orderShippingMapper.selectOrderShippingByOrderId(orderId);
        List<OrderItem> orderItems = orderItemMapper.selectOrderItemByOrderId(orderId);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        OrderDetailResponse detailResponse = new OrderDetailResponse();
        detailResponse.setOrderTotal(order.getPayment());
        detailResponse.setUserId(order.getUserId());
        detailResponse.setOrderStatus(order.getStatus());
        detailResponse.setUserName(orderShipping.getReceiverName());
        detailResponse.setTel(orderShipping.getReceiverPhone());
        detailResponse.setStreetName(orderShipping.getReceiverAddress());
        detailResponse.setGoodsList(orderItemDtos);
//        detailResponse.setUserName(member.getUsername());

        return detailResponse;
    }

    @Override
    public ShoppingOrderVO shoppingOrder(CommonBO commonBO,Long uid) {

        //里面还有一个sort排序没有用到
//        PageHelper.startPage(commonBO.getPage(),commonBO.getSize());
        Integer page = commonBO.getPage();
        Integer size = commonBO.getSize();
        String sort = commonBO.getSort();
        Integer startPage = (page - 1) * size;

        List<Order> orders  = orderMapper.selectAllOrderBySortDesc(startPage,size,sort,uid);

        ArrayList<DataDTO> data = new ArrayList<>();

        for (Order order : orders) {
            List<OrderItem> items = orderItemMapper.selectOrderItemByOrderId(order.getOrderId());

            OrderShipping shipping = orderShippingMapper.selectOrderShippingByOrderId(order.getOrderId());

            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(items);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(shipping);
            DataDTO dataDTO = orderConverter.order2DataDTO(order);
            dataDTO.setOrderItemDto(orderItemDtos);
            dataDTO.setOrderShippingDto(orderShippingDto);
            data.add(dataDTO);
        }

        ShoppingOrderVO shoppingOrderVO = new ShoppingOrderVO();
        shoppingOrderVO.setData(data);
        int total = orderMapper.countAllByUid(uid);
        shoppingOrderVO.setTotal(total);

//        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
//        long total1 = orderPageInfo.getTotal();

        return shoppingOrderVO;
    }
}
